package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.DictCityInfo;

public abstract class GetDictCityListener extends BaseListener {

	@Override
	public void onOffline() {
	}

	public abstract void onResponseSuccess(List<DictCityInfo> lists);

	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret = response.getInt("ret");
			switch (ret) {
			case 0:
				JSONObject result = response.getJSONObject("result");
				JSONArray clists = result.getJSONArray("list");
				List<DictCityInfo> lists = new ArrayList<DictCityInfo>();
				for (int i = 0; i < clists.length(); i++) {
					JSONObject obj = clists.getJSONObject(i);
					DictCityInfo mode = new DictCityInfo();
					mode.setCityid(obj.getString("city_id"));
					mode.setCityname(obj.getString("city_name"));
					lists.add(mode);
				}
				onResponseSuccess(lists);
				break;
			case 2:
				onOffline();
				break;
			case 4:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
				break;
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			onResponseFailed();
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			String responseString, Throwable throwable) {
		super.onFailure(statusCode, headers, responseString, throwable);
		onResponseFailed();
	}
}
